<?php

namespace app\data\controller\control;

use app\data\model\ControlAccessoryModel;
use app\data\model\ControlStockJournalModel;
use think\admin\Controller;
use think\facade\Db;

/**
 * 配件管理
 * Class Cate
 * @package app\data\controller\shop
 */
class Accessory extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'SnControlAccessory';

    /**
     * 配件管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = "配件管理";
        $query = $this->_query($this->table);
        // 加载对应数据
        $this->type = $this->request->get('type', 'index');
        if ($this->type === 'index') $query->where(['deleted' => 1]);
        elseif ($this->type === 'recycle') $query->where(['deleted' => 0]);
        else $this->error("无法加载 {$this->type} 数据列表！");
        // 列表排序并显示
        $query->whereRaw('char_length(material_code) = 13')->like('material_name,material_code,material_model');
        $query->equal('status')->order('material_code asc')->page();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        if ($this->request->isGet()) {


        } elseif ($this->request->isPost()) {

        }
    }

    /**
     * 添加配件
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑配件
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form', 'id');
    }

    /**
     * 更新库存
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function saveStock()
    {
        $data = input('post.');
        $material = ControlAccessoryModel::find(['id',$data['id']])->toArray();
        if($material['stock'] == $data['stock']){$this->error('数据无变化');}
        $admin_user = session('user');
        //添加入库日志
        $journalArr = array(
            'material_id'=>$data['id'],
            'cate_id'=>1,
            'material_code'=>$material['material_code'],
            'material_name'=>$material['material_name'],
            'material_model'=>$material['material_model'],
            'material_unit'=>$material['material_unit'],
            'material_price'=>$material['material_price'],
            'stock_before'=>$material['stock'],
            'stock_behind'=>$data['stock'],
            'admin_id'=>$admin_user['id'],
            'admin_name'=>$admin_user['username'],
        );
        //开启事务
        Db::startTrans();
        try{
            $res = ControlAccessoryModel::update($data);
            if(!$res){throw new \Exception("服务器繁忙");}
            $journal = ControlStockJournalModel::insert($journalArr);
            if(!$journal){throw new \Exception("服务器繁忙");}
            Db::commit();
        }catch(\Exception $e){
            // 回滚事务
            Db::rollback();
            // 获取提示信息
            $this->error($e->getMessage());die;
        }
        $this->success('数据更新成功');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {

    }


    /**
     * 删除物资分类
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_save($this->table, $this->_vali([
            'deleted.in:0,1'  => '状态值范围异常！',
            'deleted.require' => '状态值不能为空！',
        ]), 'id');
    }
}